package com.xiaoke.shiro;

import com.google.gson.Gson;
import com.xiaoke.util.common.AjaxMsg;
import com.xiaoke.util.common.ValidUtil;
import org.apache.shiro.web.filter.AccessControlFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CustomFilter extends AccessControlFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        //获取请求time,appId，如果不存在，直接返回错误提示
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

       // String time = request.getHeader("time");
        String appId = request.getHeader("appId");
       // String token = request.getHeader("token");
        if(ValidUtil.isEmpty(appId)) {
            String json = new Gson().toJson(AjaxMsg.error("request header missing necessary parameters!"));
            response.getWriter().print(json);
            return false;
        }
        return true;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        return false;
    }
}
